package com.maple.exclusivesport.ui.main;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.maple.exclusivesport.MyThread;
import com.maple.exclusivesport.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";
    @Bind(R.id.section_label)
    TextView sectionLabel;
    @Bind(R.id.recordTimeButton)
    Button recordTimeButton;
    @Bind(R.id.section_label2)
    TextView sectionLabel2;

    static PlaceholderFragment fragment;

    public static PlaceholderFragment newInstance(int index) {
        if(fragment==null){
            fragment = new PlaceholderFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(ARG_SECTION_NUMBER, index);
            fragment.setArguments(bundle);
        }

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new Handler();
        myThread = new MyThread(getContext(), (text, num) -> {
            handler.post(() -> {
                sectionLabel.setText(text);
                sectionLabel2.setText(num);
            });

        });
    }

    MyThread myThread;

    Handler handler;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_main, container, false);
        ButterKnife.bind(this, root);


        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    Boolean isStart = true;

    @OnClick(R.id.recordTimeButton)
    public void onViewClicked() {

        if (isStart) {
            isStart = false;
            //开启
            if (myThread.isPause()) {
                myThread.resumeThread();
            } else {
                myThread.start();
            }
            recordTimeButton.setText("暂停");
        } else {
            isStart = true;
            myThread.pauseThread();
            recordTimeButton.setText("开始");
        }


    }
}